using UnityEngine;
using System.Collections;

public class backClass : MonoBehaviour {
	
	/*public GameObject player;
	public GameObject bullet;
	//public Transform objectToSpawn;
	
	private RaycastHit hit;
	
	private Vector3 oX;
	private Vector3 vec1;
	private Vector3 vec2;
	
	private float angle1;
	
	public float smooth = 2.0f;
	private float BulletCurrentSpeed = 1.0f;
	private float xMove;
	private float yMove;*/
	
	//public GameObject bullet;
	public float shootCooldown = 10f;
	private float shootTimer = 0;
	
	private GameObject nBullet;
	private globalVarsClass gv;
    public OTSprite bullet;     
    public OTSprite enemy;     
    OTSprite gun;  
	//public bool shooting = false;
	
	//screen size
	public float height;
	public float width;
	
	//game pause
	public bool gameStopped = false;
	
	//spawn
	private float spawnTimer = 0;
	public float spawnDelay = 0.5f;
	
	// Use this for initialization
	void Start () {
		height = 2 * Camera.main.orthographicSize;
	  	width = height * Camera.main.aspect;
		
		//oX.Set(1, 0, 0);
		
        gun = OT.ObjectByName("Player") as OTSprite;
	}
	public void setPause(bool pause){
		if (pause){
			gameStopped = true;
		}else{
			gameStopped = false;
		}
	}

	// Update is called once per frame
	private void Update () {
		if(gameStopped) return;
		
		gv = GameObject.Find("GlobalVariables").GetComponent<globalVarsClass>();
		
		spawnTimer += Time.deltaTime;
		
		if(spawnTimer > spawnDelay){
			spawnTimer = 0;
			//GameObject.Find("MonsterSpawner").GetComponent<monsterSpawnerClass>().spawn(enemy);
		}
		
		gun.RotateTowards(OT.view.mouseWorldPosition);
		
		for(int i = 0; i < gv.bulletList.Count; i++){
			
			gv.bulletList[i].GetComponent<bulletClass>().moveMe();
			//gv.bulletList[i].transform.position = new Vector3(gv.bulletList[i].transform.position.x + gv.bulletList[i].GetComponent<bulletClass>.xMove,
			//gv.bulletList[i].transform.position.y + gv.bulletList[i].GetComponent<bulletClass>.yMove, -1);
		
			
			
			
			if(gv.bulletList[i].transform.position.x > width/2 || 
				gv.bulletList[i].transform.position.x < -width/2 || 
				gv.bulletList[i].transform.position.y > height/2 || 
				gv.bulletList[i].transform.position.y < -height/2)
			{
				Destroy(gv.bulletList[i].gameObject);
				gv.bulletList.RemoveAt(i);
				gv.bulletCount -= 1;
				i--;
			}else{
				for(int j = 0; j < gv.MobList.Count; j++){	
					if(gv.bulletList[i].transform.position.x < gv.MobList[j].transform.position.x + gv.MobList[j].rect.width/2 && 
						gv.bulletList[i].transform.position.x > gv.MobList[j].transform.position.x - gv.MobList[j].rect.width/2 && 
						gv.bulletList[i].transform.position.y < gv.MobList[j].transform.position.y + gv.MobList[j].rect.height/2 && 
						gv.bulletList[i].transform.position.y > gv.MobList[j].transform.position.y - gv.MobList[j].rect.height/2)
					{
						Destroy(gv.bulletList[i].gameObject);
						gv.bulletList.RemoveAt(i);
						gv.bulletCount -= 1;
						i--;
						Destroy(gv.MobList[j].gameObject);
						gv.MobList.RemoveAt(j);
						gv.MobCount -= 1;
						break;
					}
				}
			}
			
			/*float bulletX = gv.bulletList[i].transform.position.x;
			float bulletY = gv.bulletList[i].transform.position.y;
			if(bulletX*bulletX+bulletY*bulletY > 25){
				Destroy(gv.bulletList[i].gameObject);
				//Destroy(i);
				gv.bulletList.RemoveAt(i);
				gv.bulletCount -= 1;
				i--;
			}*/
			
		}
		for(int j = 0; j < gv.MobList.Count; j++){	
			gv.MobList[j].GetComponent<badGuyClass>().moveMe();
			if(gv.MobList[j].transform.position.x < gun.rect.width/2 && 
				gv.MobList[j].transform.position.x > -gun.rect.width/2 && 
				gv.MobList[j].transform.position.y < gun.rect.height/2 && 
				gv.MobList[j].transform.position.y > -gun.rect.height/2)
			{
				Destroy(gv.MobList[j].gameObject);
				gv.MobList.RemoveAt(j);
				gv.MobCount -= 1;
				j--;
			}
		}
		
		
		/*foreach(OTSprite i in gv.bulletList){
			if(i.transform.position.x < -4){
				Destroy(i.gameObject);
				//Destroy(i);
				gv.bulletList.Remove(i);
				gv.bulletCount -= 1;
			}
		}*/
		shootTimer += 1;
		if (Input.GetMouseButton(0) && shootTimer >= shootCooldown)  // 0- left click, 1- right, 2- middle 
        {
			shootTimer = 0;
			
			
			// bullet.rotation = gun.rotation;
	
	        // check if the left mouse button was clicked
	        
	            // Create a new bullet
				
				//nBullet = Instantiate(bullet, gun.position + (gun.yVector * (gun.size.y / 2)), gun.rotation); 
	            OTSprite nBullet = (Instantiate(bullet.gameObject) as GameObject).GetComponent<OTSprite>();
	            // Set bullet's position at approximately the gun's shooting barrel
	            nBullet.position = gun.position + (gun.yVector * (gun.size.y / 2));
	            nBullet.rotation = gun.rotation;
				
				
				/*var bull_clone : GameObject;
				bull_clone = Instantiate(bullet, transform.position, transform.rotation);
				Physics.IgnoreCollision(bull_clone.collider, collider);
				bull_clone.rigidbody.AddForce(transform.forward*bulletImpulse, ForceMode.Impulse);
				
	            nBullet.GetComponent<bulletClass>().xMove = 1;
	            nBullet.GetComponent<bulletClass>().yMove = 1;*/
	            // Play the gun's shooting animation frameset once
	            //gun.PlayOnce("shoot")
				gv = GameObject.Find("GlobalVariables").GetComponent<globalVarsClass>();
			//	gv.bulletList.Insert(1,nBullet);
				//nBullet.GetComponent<bulletClass>().xMove = 1;
				gv.bulletList.Add(nBullet);
				gv.bulletCount += 1;
			/*xMove = 0;
			yMove = 0;   
			bullet.gameObject.transform.position = new Vector3(0, 0, -1);
			
            Ray scrRay = Camera.main.ScreenPointToRay(Input.mousePosition);
			
            if (Physics.Raycast(scrRay, out hit, Mathf.Infinity)) {
				
				vec1 = hit.point - player.gameObject.transform.position;
				vec1.z = 0;
				
				angle1 = Vector3.Angle(vec1, oX);
				if (vec1.y < player.gameObject.transform.position.y)
					angle1 = -angle1;
				
				Quaternion target = Quaternion.Euler(0, 0, angle1);
        		player.gameObject.transform.rotation = Quaternion.Slerp(player.gameObject.transform.rotation, target, Mathf.Infinity);
				
				//// Shooting the bullet
				bullet.gameObject.transform.rotation = Quaternion.Slerp(player.gameObject.transform.rotation, target, Mathf.Infinity);
				bullet.gameObject.transform.position.Set(0, 0, -1);
				
				vec2 = bullet.gameObject.transform.position - hit.transform.position;
				vec2.z = -1;
		
				xMove = vec2.x * BulletCurrentSpeed * Time.deltaTime;
				yMove = vec2.y * BulletCurrentSpeed * Time.deltaTime;
				////
				
				//bullet.gameObject.transform.position = player.gameObject.transform.rotation;
			}*/
        }
		
		
		//bullet.gameObject.transform.position = new Vector3(bullet.gameObject.transform.position.x - xMove, bullet.gameObject.transform.position.y - yMove, -1);
		
	}
}
